#ifndef _TTY_H
#define _TTY_H

#include <tty_ldisc.h>

#define TTY_FLAGS_LDISC_OPENED	(0x1)
#define TTY_FLAGS_LDISC_ENABLED	(0x2)
#define TTY_FLAGS_POLL_READ		(0x4)

#define tty_flags_is_ldisc_opened(flags)	((flags) & TTY_FLAGS_LDISC_OPENED)
#define tty_flags_is_ldisc_closed(flags)	(((flags) & TTY_FLAGS_LDISC_OPENED) == 0)
#define tty_flags_is_ldisc_enabled(flags)	((flags) & TTY_FLAGS_LDISC_ENABLED)
#define tty_flags_is_ldisc_disabled(flags)	(((flags) & TTY_FLAGS_LDISC_ENABLED) == 0)
#define tty_flags_is_poll_read(flags)		((flags) & TTY_FLAGS_POLL_READ)

#define tty_flags_set_ldisc_opened(flags)	((flags) |= TTY_FLAGS_LDISC_OPENED)
#define tty_flags_set_ldisc_closed(flags)	((flags) &= ~TTY_FLAGS_LDISC_OPENED)
#define tty_flags_set_ldisc_enabled(flags)	((flags) |= TTY_FLAGS_LDISC_ENABLED)
#define tty_flags_set_ldisc_disabled(flags)	((flags) &= ~TTY_FLAGS_LDISC_ENABLED)
#define tty_flags_set_poll_read(flags)		((flags) |= TTY_FLAGS_POLL_READ)

#define tty_flags_clr_poll_read(flags)		((flags) &= ~TTY_FLAGS_POLL_READ)

struct tty {
	unsigned int flags;
	int ldisc_nr;
	void *ldisc_priv;
	struct device *dev;
};

struct tty_driver;

struct tty_operations {
    int  (*open)(struct tty *tty);
    void  (*close)(struct tty *tty);
	int  (*read)(struct tty *tty, unsigned char *buf, unsigned int count);
    int  (*write)(struct tty *tty, const unsigned char *buf, unsigned int count);
    int  (*ioctl)(struct tty *tty, unsigned int cmd, unsigned int arg);
    void (*flush)(struct tty *tty);
	int (*match)(struct device *dev, struct tty_driver *tdrv);
    int (*probe)(struct tty *tty, struct tty_driver *tdrv);
};

struct tty_driver {
    struct tty_operations *ops;
	struct driver *drv;
};

#define device_to_tty(dev)			((dev)->priv)
#define driver_to_tty_driver(drv)	((drv)->priv)
#define tty_to_device(tty)			((tty)->dev)
#define tty_driver_to_driver(tdrv)	((tdrv)->drv)

#define device_to_tty_driver(dev)	(driver_to_tty_driver(device_to_driver(dev)))
#define tty_to_driver(tty)			(device_to_driver(tty_to_device(tty)))
#define tty_to_tty_driver(tty)		(driver_to_tty_driver(tty_to_driver(tty)))

extern struct device_operations tty_ops;

#endif /* _TTY_H */

